package com.example.musicproject.Controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.dto.Register;
import com.example.common.dto.Userdto;
import com.example.common.pojo.musicuser;
import com.example.common.utils.JWTUtils;
import com.example.common.utils.R;
import com.example.service.MusicUserservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("/employee")
@Api(tags = "用户管理")
public class MusicUserController {

    @Autowired
    private MusicUserservice musicUserservice;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /*
    * 不管你是否绑定了手机号，用户都需要手机号登录
    * */
    /*
    * 1.判断用户名在数据库中是否存在，
    *   如果没有就注册，如果有就验证手机号
    * 2.判断用户是否绑定了手机号
    *   2.1 如果绑定了手机号就直接登录进去，并且返回token
    *   2.2 如果没有完成手机号收取验证码输出，到注册接口
    * */

    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "登录")
    public R<Userdto> login(@RequestBody musicuser user){
        Userdto userdto = new Userdto();
        try {
//        泛型对于的是表名
        LambdaQueryWrapper<musicuser> wapper = new LambdaQueryWrapper<>();
//        User::getUsername获取的是对象的属性名
        wapper.eq(musicuser::getName,user.getName());
        musicuser musicuserdao = musicUserservice.getOne(wapper);
        if (musicuserdao.getPhone().equals("")){
            return R.error("手机号未绑定");
        }
        if (musicuserdao.getStatus()==0){
            return R.error("账号已禁用");
        }
        Map<String, String> payload = new HashMap<>();
//        Long型转String
        payload.put("id",String.valueOf(musicuserdao.getId()));
        payload.put("name",user.getName());
        String token = JWTUtils.getToken(payload);
        userdto.setId(String.valueOf(musicuserdao.getId()));
        userdto.setState(true);
        userdto.setMessage("登录成功");
        userdto.setToken(token);
        redisTemplate.opsForValue().set(token, JSON.toJSONString(musicuserdao),7, TimeUnit.DAYS);
        }catch (NullPointerException e){
            return R.error("用户不存在");
        }catch (Exception e){
            userdto.setState(false);
            userdto.setMessage("登录失败,请联系管理员!");
            userdto.setToken(null);
            e.printStackTrace();
        }
        return R.success(userdto);
    }

    @GetMapping("/info")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header",example = "token")
    })
    public R<musicuser> loginfo(@RequestHeader("token") String token){
//        从reids中获取信息，返回给前端
//        TODO 直接从redis中根据token获取用户信息
        String userinfo = redisTemplate.opsForValue().get(token);
        musicuser musicuserinfo = JSON.parseObject(userinfo,musicuser.class);
        return R.success(musicuserinfo);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "退出登录", notes = "退出登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header",example = "token")
    })
    public R<String> loginout(@RequestHeader("token") String token){
        redisTemplate.delete(token);
        return R.success("退出成功！");
    }

    /*
    *
    * */
    @PostMapping("/register")
    @ApiOperation(value = "注册", notes = "注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "register", value = "注册信息", required = true, dataType = "Register", paramType = "body",example = "register")
    })
    public R<String> register(@RequestBody Register register){
        R<String> registerservicetip = musicUserservice.registerservice(register);
        return registerservicetip;
    }
}
